package com.cnpinxi.service;

import com.cnpinxi.dao.AdminMenuMapper;
import com.cnpinxi.domain.AdminMenu;
import com.cnpinxi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew  on 2017/2/22.
 */
@Service
public class MenuServiceImpl implements com.cnpinxi.service.MenuService {

    @Autowired
    AdminMenuMapper adminMenuMapper;


    @Override
    public List<AdminMenu> getMenuList(Map<String, String> map) {
        return adminMenuMapper.selectMenuList(map);
    }

    @Override
    public List<Map<String, Object>> selectMenuListForRole(Map<String, String> map) {
        return adminMenuMapper.selectMenuListForRole(map);
    }

    @Override
    public void saveMenuRole(Map<String, String> map) {


    }

    @Override
    public void addMenu(Map<String, String> map) {
        String menuParentId = map.get("menuParentId");
        String url = map.get("url");
        String menuName = map.get("menuName");
        AdminMenu adminMenu = new AdminMenu();
        adminMenu.setMenuUrl(url);
        adminMenu.setMenuName(menuName);
        adminMenu.setMenuParentId(Integer.parseInt(menuParentId));
        adminMenuMapper.insertAdminMenu(adminMenu);
    }

    @Override
    public void deleteMenu(Map<String, String> map) {
        String menuIds = map.get("menuIds");
        String menuIdArry[] = menuIds.split(",");
        for (String menuId : menuIdArry) {
            AdminMenu adminMenu = adminMenuMapper.selectMenuById(Integer.parseInt(menuId));
            Integer menuParentId = adminMenu.getMenuParentId();
            Map<String, String> deletemap = new HashMap<>();
            if (menuParentId.intValue() == 0) {
                deletemap.put("menuParentId", String.valueOf(adminMenu.getTid()));
                adminMenuMapper.deleteAdminMenu(deletemap);
            }
            deletemap.clear();

            deletemap.put("tid", String.valueOf(adminMenu.getTid()));
            adminMenuMapper.deleteAdminMenu(deletemap);
        }
    }
}
